package www.openteam.com.model.business;

import www.openteam.com.OTApplication;
import www.openteam.com.base.BaseModel;
import www.openteam.com.bean.ApprovalStatu;
import www.openteam.com.exception.ApiException;
import www.openteam.com.subscribe.CommonSubscriber;
import www.openteam.com.transformer.CommonTransformer;

/**
 * Created by kylin on 2016/12/8.
 */

public class NewBuildCustomOrderInfoModel extends BaseModel {

    private boolean isInsert = false;
    private boolean isUpdate = false;

    //新建主订单
    public boolean insertCustomOrderMainInfo(
            String custid, String businessno,
            String userid,
            String moneytype, String paytime
            , String sendtype, String custorderid
            , String alreadlypay, String inputdate
            , String inputer, String orderamount
            , String remark, String orderdate
            , String sendaddress, String istax
            , String taxrate, String invtype
            , String stype, final InsertCustomOrderInfoOnListener insertCustomOrderInfoOnListener
    ) {
        httpApiService.insertCustomOrderMainInfo(custid, businessno, userid, moneytype, paytime, sendtype, custorderid, alreadlypay, inputdate, inputer, orderamount, remark, orderdate, sendaddress, istax, taxrate, invtype, stype)
                .compose(new CommonTransformer<ApprovalStatu>())
                .subscribe(new CommonSubscriber<ApprovalStatu>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(ApprovalStatu approvalStatu) {
                        isInsert = true;
                        insertCustomOrderInfoOnListener.insertCOISuccess(approvalStatu);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isInsert = false;
                        insertCustomOrderInfoOnListener.insertdCOIFail(e);
                    }
                });
        return isInsert;
    }


    //新建子订单
    public boolean insertCustomOrderItemInfo(
            int id, String prodno
            , String wraptype, String paperinf
            , String senddate, String technicsinf
            , String printcolor, String paynumb
            , String othernumb, String price, final InsertCustomOrderItemInfoOnListener insertCustomOrderItemInfoOnListener

    ) {
        httpApiService.insertCustomOrderItemInfo(id, prodno, wraptype, paperinf, senddate, technicsinf, printcolor, paynumb, othernumb, price)
                .compose(new CommonTransformer<ApprovalStatu>())
                .subscribe(new CommonSubscriber<ApprovalStatu>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(ApprovalStatu approvalStatu) {
                        isInsert = true;
                        insertCustomOrderItemInfoOnListener.insertCOIISuccess(approvalStatu);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isInsert = false;
                        insertCustomOrderItemInfoOnListener.insertCOIIFail(e);
                    }
                });
        return isInsert;
    }

    //插入主订单
    public interface InsertCustomOrderInfoOnListener {
        void insertCOISuccess(ApprovalStatu approvalStatu);

        void insertdCOIFail(Throwable throwable);

    }

    //插入子订单
    public interface InsertCustomOrderItemInfoOnListener {
        void insertCOIISuccess(ApprovalStatu approvalStatu);

        void insertCOIIFail(Throwable throwable);

    }
}
